package top.appx.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lhzxd on 2017/5/3.
 */
@Aspect
@Component
public class HttpAspect {
    private static final Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * top.appx.controller.*.*(..))")
    public void log(){
    }
    @AfterReturning(pointcut = "log()",returning = "object")
    public void rt(JoinPoint joinPoint, Object object) throws IOException {

        Class returnType = ((MethodSignature)joinPoint.getSignature()).getReturnType();
        if(returnType == void.class){
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletResponse response = attributes.getResponse();
            response.getWriter().write("{\"success\":true}");
        }

    }

}
